package ex1;

public class SynchronizedQueue<T> extends Queue<T> {
	
	/**
     * Enqueue - adds item to queue (SYNCHRONIZED)
     * @param item - item to add to queue
     */
    public synchronized boolean enqueue(T item) {
    	Node node = new Node();
        node.item = item;
        if (isEmpty()) {
        	first = node;
        	last = node;
        } else {
        	last.next = node;
        	last = node;
        }
        size++;
    	notify();
        return true; // succeeded
    }

    /**
     * Dequeue - remove first item from queue (SYNCHRONIZED) 
     * @return first item of queue that has been removed
     */
    public synchronized T dequeue() {
    	while (isEmpty()) {
        	try {
       			wait();
			} catch (InterruptedException e) {
			}
        }
        T item = first.item;
        first = first.next;
        size--;
        if (isEmpty()) {
        	last = null;
        }
    	notify();
        return item; // succeeded
    }
}
